/*
 * ﻿Copyright (C) 2009-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.gwetons.client.logging;

import com.google.gwt.core.client.GWT;
import com.newmainsoftech.gwetons.shared.logging.LoggerWrap;
import com.newmainsoftech.gwetons.shared.logging.LoggerWrapFactoryImpl;
import com.newmainsoftech.gwetons.shared.logging.LoggerWrapFactoryInstanceHolder;

public class JsniLoggerWrapFactoryImpl extends LoggerWrapFactoryImpl implements JsniLoggerWrapFactory {
	private static JsniLoggerWrapGinjector jsniLoggerWrapGinjector;
		public static JsniLoggerWrapGinjector getJsniLoggerWrapGinjector() {
			if ( JsniLoggerWrapFactoryImpl.jsniLoggerWrapGinjector == null) {
				if ( GWT.isClient()) {
					JsniLoggerWrapGinjector jsniLoggerWrapGinjectorObj 
					= GWT.create( JsniLoggerWrapGinjector.class);
					JsniLoggerWrapFactoryImpl.setJsniLoggerWrapGinjector( jsniLoggerWrapGinjectorObj);
				}
				else {
					throw new UnsupportedOperationException(
							"Usage is limited only for client side because this is factory for %1$s "
							.concat( "instance what aims the usage only for client side. For non-client ")
							.concat( "side, use %2$s instead.") 
							.replace( "%1$s", JsniLoggerWrap.class.getName())
							.replace( "%2$s", LoggerWrapFactoryImpl.class.getName())
							);
				}
			}
			return JsniLoggerWrapFactoryImpl.jsniLoggerWrapGinjector;
		}
		public static void setJsniLoggerWrapGinjector( JsniLoggerWrapGinjector jsniLoggerWrapGinjector) {
			JsniLoggerWrapFactoryImpl.jsniLoggerWrapGinjector = jsniLoggerWrapGinjector;
		}
	
	static String getLoggerName() {
		return JsniLoggerWrapFactoryImpl.class.getName();
	}
	private static JsniLoggerWrap getThisJsniLoggerWrap() {
		LoggerWrap loggerWrapObj 
		= LoggerWrapFactoryInstanceHolder.getLoggerWrapFactory().getLoggerWrap( getLoggerName());
		
		JsniLoggerWrap jsniLoggerWrap = getJsniLoggerWrapGinjector().getJsniLoggerWrap();
			jsniLoggerWrap.setLoggerWrap( loggerWrapObj);
		
		return jsniLoggerWrap;
	}

	protected JsniLoggerWrapFactoryImpl() {
	}
	
	public static JsniLoggerWrap getJsniLoggerWrap( 
			JsniLoggerWrap instance, LoggerWrap loggerWrap, 
			String loggerName, JsniLoggerWrapFactory jsniLoggerWrapFactory) {
		String methodName = "getJsniLoggerWrap";
		LoggerWrap thisLoggerWrap = JsniLoggerWrapFactoryImpl.getThisJsniLoggerWrap().getLoggerWrap();
		thisLoggerWrap.entering( 
				methodName, 
				new Object[]{ instance, loggerWrap, loggerName, jsniLoggerWrapFactory}
				);
		
		if ( instance != null) {
			thisLoggerWrap.exiting( methodName, instance);
			return instance;
		}
		
		JsniLoggerWrapFactory jsniLoggerWrapFactoryObj = jsniLoggerWrapFactory;
			if ( jsniLoggerWrapFactoryObj == null) {
				jsniLoggerWrapFactoryObj = JsniLoggerWrapFactoryInstanceHolder.getJsniLoggerWrapFactory();
			}
		LoggerWrap loggerWrapObj = loggerWrap;
			if ( loggerWrapObj == null) {
				loggerWrapObj = jsniLoggerWrapFactoryObj.getLoggerWrap( loggerName);
			}
		JsniLoggerWrap jsniLoggerWrapObj = jsniLoggerWrapFactoryObj.getJsniLoggerWrap( loggerWrapObj);
		
		thisLoggerWrap.exiting( methodName, jsniLoggerWrapObj);
		return jsniLoggerWrapObj;
	}
	
	// JsniLoggerWrapFactoryInterface methods -----------------------------------------------------
	public JsniLoggerWrap getJsniLoggerWrap( LoggerWrap loggerWrap) {
		String methodName = "getJsniLoggerWrap";
		LoggerWrap thisLoggerWrap = JsniLoggerWrapFactoryImpl.getThisJsniLoggerWrap().getLoggerWrap();
		thisLoggerWrap.entering( methodName, new Object[]{ loggerWrap});
		
		JsniLoggerWrap jsniLoggerWrap = JsniLoggerWrapFactoryImpl.getJsniLoggerWrapGinjector().getJsniLoggerWrap();
			jsniLoggerWrap.setLoggerWrap( loggerWrap);
		
		thisLoggerWrap.exiting( methodName, jsniLoggerWrap);
		return jsniLoggerWrap;
	}
	
	public JsniLoggerWrap getJsniLoggerWrap( String loggerName) {
		String methodName = "getJsniLoggerWrap";
		LoggerWrap thisLoggerWrap = JsniLoggerWrapFactoryImpl.getThisJsniLoggerWrap().getLoggerWrap();
		thisLoggerWrap.entering( methodName, new Object[]{ loggerName});

		LoggerWrap loggerWrap = LoggerWrapFactoryImpl.getLoggerWrapInjector().getLoggerWrap();
			loggerWrap.setUpLogger( loggerName);
		
		JsniLoggerWrap jsniLoggerWrap = getJsniLoggerWrap( loggerWrap);

		thisLoggerWrap.exiting( methodName, jsniLoggerWrap);
		return jsniLoggerWrap;
	}
	// --------------------------------------------------------------------------------------------
}
